package com.github.niefy.modules.wx.service.impl;

import com.github.niefy.modules.wx.service.MassTextService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
import me.chanjar.weixin.mp.bean.WxMpMassTagMessage;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.github.niefy.modules.wx.service.MsgReplyService.logger;

@Service
public class MassTextServiceImpl implements MassTextService {
    @Autowired
    WxMpService wxMpService;

    /**
     * 根据openid集合群发消息
     * @param content
     * @param openids
     * @param type
     * @return
     */
    @Override
    public WxMpMassSendResult massSendByOpenid(String content, String[] openids, String type) {
        return massSendByOpenid(type,content,openids);
    }

    /**
     * 根据tagid群发消息
     * @param content 消息内容，当type=text时，content是文本内容，其他类型时时
     * @param tagId
     * @param type
     * @return
     */
    @Override
    public WxMpMassSendResult massSendByTagId(String content, Long tagId, String type) {
        return massSendByTag(type,content,tagId);
    }

    /**
     * 根据openid群发消息
     * @param msgType 消息类型（text,image,mpnews,mpvideo,voice)
     * @param content
     * @param msgType
     * @return
     */
    private WxMpMassSendResult massSendByOpenid(String msgType,String content,String[] openids){
        WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
        massMessage.setMsgType(msgType);
        for (String openid : openids) {
            massMessage.getToUsers().add(openid);
        }
        switch (msgType){
            // 发送文本消息
            case WxConsts.MassMsgType.TEXT:
                massMessage.setContent(content);
                break;
            // 发送除文本消息之外的消息（群发格式一致）
            case WxConsts.MassMsgType.IMAGE:
            case WxConsts.MassMsgType.MPNEWS:
            case WxConsts.MassMsgType.MPVIDEO:
            case WxConsts.MassMsgType.VOICE:
                massMessage.setMediaId(content);
                break;
            default:
                break;
        }
        try {
            return wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return null;
    }

    /**
     * 根据tagid群发消息
     * @param msgType 消息类型（text,image,mpnews,mpvideo,voice)
     * @param content
     * @param tagId
     * @return
     */
    private WxMpMassSendResult massSendByTag(String msgType,String content,Long tagId){
        WxMpMassTagMessage massMessage = new WxMpMassTagMessage();
        massMessage.setMsgType(msgType);
        massMessage.setTagId(tagId);
        switch (msgType){
            case WxConsts.MassMsgType.TEXT:
                massMessage.setContent(content);
                break;
            // 发送除文本消息之外的消息（群发格式一致）
            case WxConsts.MassMsgType.IMAGE:
            case WxConsts.MassMsgType.MPNEWS:
            case WxConsts.MassMsgType.MPVIDEO:
            case WxConsts.MassMsgType.VOICE:
                massMessage.setMediaId(content);
                break;
            default:
                break;
        }
        massMessage.setMediaId(content);
        try {
            return wxMpService.getMassMessageService().massGroupMessageSend(massMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return null;
    }
}
